package com.reading.cms.controller.system;

import com.reading.cms.controller.BaseController;
import com.reading.common.cache.service.CacheService;
import com.reading.common.entity.system.Menu;
import com.reading.common.entity.system.SysUser;
import com.reading.core.manager.system.IMenuManager;
import com.reading.core.manager.system.ISystemUserManager;
import com.reading.common.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 首页 业务处理
 *
 * @author ruoyi
 */
@Slf4j
@Controller
public class IndexController extends BaseController {

    @Resource
    private IMenuManager menuManager;

    @Resource
    private ISystemUserManager userManager;

    @Resource
    private CacheService cacheService;

    /**
     * 系统首页
     */
    @GetMapping({"/index", "/", ""})
    public String index(Model model) {
        // 取身份信息
        SysUser sysUser = userManager.selectUserById(getUserId());
        // 根据用户id取出菜单
        List<Menu> menus = menuManager.selectMenusByUserId(sysUser.getUserId());
        model.addAttribute("menus", menus);
        model.addAttribute("user", sysUser);
        model.addAttribute("copyrightYear", "2018");
        return "index";
    }

    /**
     * 系统主页
     */
    @GetMapping("/system/main")
    public String main(Model model) {
        model.addAttribute("version", "v1.0");
        model.addAttribute("user", getUser());
        return "main";
    }

    @RequiresPermissions("system:msg:view")
    @GetMapping("/system/msgList")
    @ResponseBody
    public JsonResult msgList() {
        JsonResult result = success();
        try {
            Integer all = 0;
            List<String> agentLogs = cacheService.getPlayerAgentLog();
            List<String> playerExchangeInfos = cacheService.getPlayerExchangeInfo();
            Set<String> feedbackProblemLogs = cacheService.getPlayerFeedbackProblemLog();
            all = agentLogs.size() + playerExchangeInfos.size() + feedbackProblemLogs.size();

            result.put("agentLogs", agentLogs);
            result.put("playerExchanges", playerExchangeInfos);
            result.put("feedbackProblemLogs", feedbackProblemLogs);
            result.put("msgCount", all);
        } catch (Exception e) {
            log.error("从缓存中获取提示消息失败，", e);
            result = error();
        }
        return result;
    }

    @GetMapping("/system/totalCount")
    @ResponseBody
    public JsonResult totalCount() {
        JsonResult result = success();
        try {


        } catch (Exception e) {
            log.error("查询首页数据异常", e);
            result = error();
        }
        return result;
    }
}
